package utils;

import java.awt.Component;
import java.awt.Container;
import java.util.ArrayList;
import java.util.List;

import constants.GlobalDebug;
import controller.ShapeController;
import figures.Line;
import figures.Move;
import figures.Oval;
import figures.Rectangle;
import figures.SelectBox;
import figures.ShapeContainer;
import figures.Smily;
import figures.WBShape;

public class StringReadWrite {

    public static void loadFromFile(Container parent,
	    ShapeController controler, String filname) {
	//FP
	controler.clearAll();
	ShapeContainer sc_ = new ShapeContainer();
	List<String> lines = StringReader.read(filname);
	for (String line : lines) {
	    WBShape c = null;
	    if (line.contains("Smily"))
		c = new Smily();
	    else if (line.contains("Line"))
		c = new Line();
	    else if (line.contains("Oval"))
		c = new Oval();
	    else if (line.contains("Rectangle"))
		c = new Rectangle();
	    else if (line.contains("SelectBox"))
		c = new SelectBox();
	    else if (line.contains("Move"))
		c = new Move();
	    if (c != null) {
		c.parse(line);
		sc_.add(c);
	    }
	}
	Component[] arrayShapes = sc_.getComponents();
	if (GlobalDebug.isOn)
	    System.out.println("Read "
		    + arrayShapes.length + " shapes from the file");
	for (int i = 0; i < arrayShapes.length; i++) {
	    WBShape s = (WBShape) arrayShapes[i];
	    parent.add(s);
	    s.addMouseListener(controler);
	    s.addMouseMotionListener(controler);
	    if (GlobalDebug.isOn)
		 System.out.println(s.asString());
	}
	parent.repaint();
    }
    
    
    public static void createShape(Container parent,
	    ShapeController controler, String args) {
	//controler.clearAll();
	//ShapeContainer sc = new ShapeContainer();
	 WBShape c = null;
	    if (args.contains("Smily"))
		c = new Smily();
	    else if (args.contains("Line"))
		c = new Line();
	    else if (args.contains("Oval"))
		c = new Oval();
	    else if (args.contains("Rectangle"))
		c = new Rectangle();
	    else if (args.contains("SelectBox"))
		c = new SelectBox();
	    else if (args.contains("Move"))
		c = new Move();
	    if (c != null) {
		c.parse(args);
		parent.add(c);
		if (GlobalDebug.isOn)
		  System.out.println("Created "
			    + c.asString());
		c.addMouseListener(controler);
		c.addMouseMotionListener(controler);
		parent.repaint();
	    }
    }


    public static void saveToFile(Container parent, String filname) { 
	List<String> lines = new ArrayList<String>();
	ShapeContainer sc = new ShapeContainer();
	Component[] arrayShapes = parent.getComponents();
	for (int i = 0; i < arrayShapes.length; i++) {
	    WBShape s = (WBShape) ((WBShape) arrayShapes[i]).clone();
	    s.setSelectState(false);
	    sc.add(s);
	    lines.add(s.asString());
	}
	if (GlobalDebug.isOn)
	    System.out.println("Written "
		    + parent.getComponentCount() + " shapes to the file");
	StringWriter.write(filname, lines); 
    }

    
}
